package com.ning.activitys.skysound.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.google.gson.Gson;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.activity.MainActivity;
import com.ning.activitys.skysound.base.BaseFragment;
import com.ning.activitys.skysound.beans.GsonFindMusic;
import com.ning.activitys.skysound.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class FindMusicFragment extends BaseFragment{

    private Gson gson = new Gson();
    private GsonFindMusic gsonFindMusic;

    private MainActivity mainActivity;
    private GsonFindMusic.DataEntity data;
    private  List<GsonFindMusic.DataEntity> findMusicsData = new ArrayList<>();

    private ExpandableListView expandableListView;
    private FindExAdapter adapter;

    private String url  = "http://api.dongting.com/frontpage/frontpage?location=0&version=1453541260911&app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=9.2&active=1&net=2&openudid=1320e380b9b2f879587531a841bd2709b0fe0b43&idfa=1797AE83-E11D-40E8-BF4A-51688F9D8197&utdid=VcqZd7Bai40DAKYNvRHg4TLh&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude=";


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_found_music,null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        expandableListView = (ExpandableListView) view.findViewById(R.id.expandable_list_view_find_music);
        getNetDate();
    }

    private void getNetDate() {

        VolleySingleton.addRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                gsonFindMusic = gson.fromJson(response,GsonFindMusic.class);

                for (int i = 3; i < gsonFindMusic.getData().size(); i++) {

                    data = new GsonFindMusic.DataEntity();

                    data = gsonFindMusic.getData().get(i);

                    findMusicsData.add(data);
                }

                adapter = new FindExAdapter();
                expandableListView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

    }


    public class FindExAdapter extends BaseExpandableListAdapter{

        @Override
        public int getGroupCount() {
            return findMusicsData.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return findMusicsData.get(groupPosition).getData().size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return null;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return null;
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupViewHolder groupViewHolder;
            if (convertView == null){
                convertView = LayoutInflater.from(mainActivity).inflate(R.layout.item_rv_group_radio,parent,false);
                groupViewHolder = new GroupViewHolder(convertView);
                convertView.setTag(groupViewHolder);
            }else {
                groupViewHolder = (GroupViewHolder) convertView.getTag();
            }
            groupViewHolder.groupText.setText(findMusicsData.get(groupPosition).getName());

            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }


    class GroupViewHolder{
        private TextView groupText;
        public GroupViewHolder(View view){
            groupText = (TextView) view.findViewById(R.id.tv_rv_group_radio);
        }
    }

    class ChildViewHolderV{
        private NetworkImageView imageV;
        private TextView textV;

        public ChildViewHolderV(View view){
            imageV = (NetworkImageView) view.findViewById(R.id.singer_image_singer_fragment);
            textV = (TextView) view.findViewById(R.id.tv_singer_name_singer_fragment);
        }
    }

    class ChidViewHolderH{
        private NetworkImageView imageH;
        private TextView textUpH,textBelowH;
        public ChidViewHolderH(View view){
            imageH = (NetworkImageView) view.findViewById(R.id.iv_item_image_horizontal_find_music);
            textUpH = (TextView) view.findViewById(R.id.tv_item_horizontal_up_find_music);
            textBelowH = (TextView) view.findViewById(R.id.tv_item_horizontal_below_find_music);
        }
    }
}
